<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 * User_Autologin
 *
 * This model represents user autologin data. It can be used
 * for user verification when user claims his autologin passport.
 * Model นี้ใช้เพื่อจัดการ autologin โดยใช้สำหรับยืนยัน user ที่แจ้งสถานะ auto login เข้ามา
 *
 * @package	Tank_auth
 * @subpackage Model
 * @author	Ilya Konyukhov (http://konyukhov.com/soft/)
 */
class User_Autologin extends Model {

    /**
     *
     * @var string table ที่เก็บ auto login
     */
    private $table_name = 'mem_autologin';
    /**
     *
     * @var string table ที่เก็บ member system
     */
    private $users_table_name = 'mem_system';
    /**
     * initial เรียก prefix database ขึ้นมา
     */
    function __construct() {
        parent::__construct();

        $ci = & get_instance();
        $this->table_name = $ci->config->item('db_table_prefix', 'tank_auth') . $this->table_name;
        $this->users_table_name = $ci->config->item('db_table_prefix', 'tank_auth') . $this->users_table_name;
    }

    /**
     * Get user data for auto-logged in user.
     * Return NULL if given key or user ID is invalid.
     * เรียก user row จาก user ที่ autologin
     *
     *
     * @param	int userid
     * @param	string key สำหรับ autologin
     * @return	object
     */
    function get($user_id, $key) {
        $this->db->select($this->users_table_name . '.id');
        $this->db->select($this->users_table_name . '.username');
        $this->db->from($this->users_table_name);
        $this->db->join($this->table_name, $this->table_name . '.user_id = ' . $this->users_table_name . '.id');
        $this->db->where($this->table_name . '.user_id', $user_id);
        $this->db->where($this->table_name . '.key_id', $key);
        $query = $this->db->get();
        if ($query->num_rows() == 1)
            return $query->row();
        return NULL;
    }

    /**
     * Save data for user's autologin
     * บันทึกข้อมูล autologin ของ user และ key
     *
     * @param	int userid
     * @param	string key
     * @return	bool
     */
    function set($user_id, $key) {
        return $this->db->insert($this->table_name, array(
            'user_id' => $user_id,
            'key_id' => $key,
            'user_agent' => substr($this->input->user_agent(), 0, 149),
            'last_ip' => $this->input->ip_address(),
        ));
    }

    /**
     * Delete user's autologin data
     *
     * @param	int
     * @param	string
     * @return	void
     */
    function delete($user_id, $key) {
        $this->db->where('user_id', $user_id);
        $this->db->where('key_id', $key);
        $this->db->delete($this->table_name);
    }

    /**
     * Delete all autologin data for given user
     *
     * @param	int
     * @return	void
     */
    function clear($user_id) {
        $this->db->where('user_id', $user_id);
        $this->db->delete($this->table_name);
    }

    /**
     * Purge autologin data for given user and login conditions
     * ลบข้อมูล autologin ของ user id นี้ (เพื่อไม่ให้เกิด autologin ซ้ำซ้อน)
     *
     * @param	int userid
     * @return	void
     */
    function purge($user_id) {
        $this->db->where('user_id', $user_id);
        $this->db->where('user_agent', substr($this->input->user_agent(), 0, 149));
        $this->db->where('last_ip', $this->input->ip_address());
        $this->db->delete($this->table_name);
    }

}

/* End of file user_autologin.php */
/* Location: ./application/models/auth/user_autologin.php */